#ifndef SOLUTION_H_
#define SOLUTION_H_
#include <string>
#include <vector>
#include <algorithm>
#include <map>
using namespace std;

class Solution {
public:
	struct HashCode{
		size_t num[26];
	};

	struct HCComp{
	public:
		bool operator()(const HashCode& hc1, const HashCode& hc2){
			for(int i=0;i<26;i++){
				if(hc1.num[i] < hc2.num[i])
					return true;
				else if(hc1.num[i] > hc2.num[i])
					return false;
			}
			return false;
		}
	};
    vector<string> anagrams(vector<string> &strs) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
		typedef map<HashCode,vector<size_t>,HCComp> HSMap; 
		typedef HSMap::iterator HSMapIte;
		vector<string> result;

		HashCode tmpHash;
		HSMap hcs;
		for(size_t i = 0;i<strs.size();i++){
			hashStr(strs[i],tmpHash);
			hcs[tmpHash].push_back(i);
		}

		HSMapIte ite = hcs.begin();
		HSMapIte iteEnd = hcs.end();
		while(ite  != iteEnd){
			if(ite->second.size() > 1){
				vector<size_t>& ss = ite->second;
				for(size_t i=0;i<ss.size();i++)
					result.push_back(strs[ss[i]]);
			}
			ite++;
		}
		

		
		return result;
    }

	void hashStr(const string& str, HashCode& hc){
		memset(hc.num,0,sizeof(hc.num));
		for(size_t i=0;i < str.size();i++)
			hc.num[str[i]-'a']++;
	}
};
#endif